package com.itheima.config;

import com.itheima.auth.filter.JwtAuthenticationTokenFilter;
import com.itheima.auth.handle.MyAccessDeniedHandler;
import com.itheima.auth.handle.MyAuthenticationFailureHandler;
import com.itheima.auth.handle.MyAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Resource
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Resource
    private MyAccessDeniedHandler myAccessDeniedHandler;

    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }


    @Override
    //一般建议配置静态资源
    public void configure(WebSecurity web) throws Exception {
        //swagger-ui放行
        web.ignoring().antMatchers("/v2/api-docs",
                "/webjars/**",
                "/swagger-resources/configuration/ui",
                "/swagger-resources",
                "/swagger-resources/configuration/security",
                "/swagger-ui.html"
//                "/report/**"
        );
    }

    @Override
    //一般建议配置url接口地址
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                //登入认证的处理
                    .successHandler(myAuthenticationSuccessHandler) //成功处理
                    .failureHandler(myAuthenticationFailureHandler) //失败的处理
                .and()
                //调用其它接口时出现错误的处理逻辑 将403界面替换成自定义的界面
                .exceptionHandling().accessDeniedHandler(myAccessDeniedHandler)
                .and()
                .authorizeRequests()
                //登入相关放行
                .antMatchers("/*login*").permitAll()
//                .antMatchers("/*/exportBusinessReport").permitAll()
                //剩余的url请求需要登入
                .anyRequest().authenticated()
//                .anyRequest().permitAll()
                .and()
                //关闭csrf安全认证，login接口少匹配_csrf
                .csrf().disable()
                //禁用session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                //针对的是访问url地址
                //在权限校验前加一个过滤器，验证前端传来的token是否包含数据库中存在的用户名
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
